#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QDebug"
#include <QPushButton>
#include <QLabel>

#include <QDesktopServices>
#include <QUrl>

#include "mbutton.h"
#include "config.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    win_init();

    CONFIG text;
    text.getJson_from_file();

    connect(ui->http,SIGNAL(clicked()),this,SLOT(http_button_slot()));
    connect(ui->tool,SIGNAL(clicked()),this,SLOT(tool_button_slot()));
    connect(ui->ai,SIGNAL(clicked()),this,SLOT(ai_button_slot()));
}

MainWindow::~MainWindow()
{
    config.listToJson(HTTP_JSON,http_w->get_list());
    config.listToJson(TOOL_JSON,tool_w->get_list());
    config.listToJson(AI_JSON,ai_w->get_list());
    config.updataJson_to_File();

    delete http_w;
    delete tool_w;
    delete ai_w;
    delete ui;
}

void MainWindow::win_init()
{
    //设置按钮大小和标题
    ui->http->setFixedSize(40,80);
    ui->http->setText("网页");

    ui->tool->setFixedSize(40,80);
    ui->tool->setText("软件");

    ui->ai->setFixedSize(40,80);
    ui->ai->setText("AI");

    //字体大小
    QFont font;
    font.setPointSize(12);

    config.getJson_from_file();

    http_w = new Button_Win();
    http_w->setFont(font);
    http_w->set_winname("http");
    http_w->set_list(config.get_list(HTTP_JSON));
    http_w->Button_Win_init();

    tool_w = new Button_Win();
    tool_w->setFont(font);
    tool_w->set_winname("tool");
    tool_w->set_list(config.get_list(TOOL_JSON));
    tool_w->Button_Win_init();

    ai_w = new Button_Win();
    ai_w->setFont(font);
    ai_w->set_winname("ai");
    ai_w->set_list(config.get_list(AI_JSON));
    ai_w->Button_Win_init();

    ui->stackedWidget->addWidget(http_w);
    ui->stackedWidget->addWidget(tool_w);
    ui->stackedWidget->addWidget(ai_w);
}

void MainWindow::http_button_slot()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::tool_button_slot()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::ai_button_slot()
{
    ui->stackedWidget->setCurrentIndex(2);
}




